import axios from "axios"
import {
  getLyric
} from '@/api/song'
import {
  ERR_OK
} from '@/api/config'
export default class Song {
  //                              专辑，长度 
  constructor({
    id,
    mid,
    singer,
    name,
    album,
    duration,
    img,
    url
  }) {
    this.id = id
    this.mid = mid
    this.singer = singer
    this.name = name
    this.album = album
    this.duration = duration
    this.img = img
    this.url = url
  }
  getLyric() {
    if (this.lyric) {
      return Promise.resolve(this.lyric)
    }
    return new Promise((resolve, reject) => {
      getLyric(this.mid).then((res) => {
        // console.log(res);
        if (res.data.retcode === ERR_OK) {
          this.lyric = res.data.lyric
          resolve(this.lyric);
        } else {
          reject('no lyric')
        }
      })
    })
  }
}
export function createSong(musicData) {
  return new Song({
    id: musicData.songid,
    mid: musicData.songmid,
    singer: filterSinger(musicData.singer),
    name: musicData.songname,
    album: musicData.albumname,
    duration: musicData.interval,
    img: `https://y.gtimg.cn/music/photo_new/T002R300x300M000${musicData.albummid}.jpg?max_age=2592000`,
    url: `/song?id=${musicData.songmid}`
  })
}

export function filterSinger(singer) {
  let ret = []
  if (!singer) {
    return ''
  }
  singer.forEach((item) => {
    ret.push(item.name)
  })
  return ret.join('/')
}

export function getsong(url) {
  return axios.get(url)
}